package controllers.customer;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.AuctionService;
import services.BidService;
import services.PaintingService;
import controllers.AbstractController;
import domain.Auction;
import domain.Bid;
import domain.Painting;

@Controller
@RequestMapping("/auction/customer")
public class AuctionCustomerController extends AbstractController {

	// Supporting services ----------------------------------------------------

	@Autowired
	private AuctionService auctionService;
	
	@Autowired
	private PaintingService paintingService;

	@Autowired
	private BidService bidService;
	
	// Constructors -----------------------------------------------------------

	public AuctionCustomerController() {
		super();
	}

	// Listing ---------------------------------------------------------------

	@RequestMapping("/listActiveCreatedByOthers")
	public ModelAndView listActiveCreatedByOthers() {
		ModelAndView result;

		result = new ModelAndView("auction/list");

		result.addObject("auctions",
				auctionService.findAllActiveAuctionsCreatedByOtherCustomers());
		result.addObject("URI",
				"/auction/customer/listActiveCreatedByOthers.do");

		return result;
	}
	
	@RequestMapping("/listPastByPrincipal")
	public ModelAndView listPastByPrincipal(@RequestParam(required=false) String message){
		ModelAndView result;

		result = new ModelAndView("auction/list");

		result.addObject("auctions",auctionService.findPastAuctionsByPrincipal());
		result.addObject("URI","/auction/customer/listPastByPrincipal.do");
		result.addObject("message", message);

		return result;
	}

	// Creation ---------------------------------------------------------------

	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView create(@RequestParam int paintingId) {
		ModelAndView result;

		Painting painting = paintingService.findOne(paintingId);
		Auction auction = auctionService.create(painting);
		result = createEditModelAndView(auction);

		return result;
	}

	// Edition ----------------------------------------------------------------

	@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "save")
	public ModelAndView save(@Valid Auction auction, BindingResult bindingResult) {
		ModelAndView result;

		if (bindingResult.hasErrors()) {
			result = createEditModelAndView(auction);

		} else {
			try {
				auctionService.save(auction);
				result = new ModelAndView("redirect:/painting/customer/listCreated.do");
				result.addObject("message", "auction.commit.success");
			} catch (Throwable oops) {
				result = createEditModelAndView(auction, oops.getMessage());
			}
		}

		return result;
	}

	// Ancillary methods ------------------------------------------------------

	protected ModelAndView createEditModelAndView(Auction auction) {
		ModelAndView result;

		result = createEditModelAndView(auction, null);

		return result;
	}

	protected ModelAndView createEditModelAndView(Auction auction, String message) {
		ModelAndView result;

		result = new ModelAndView("auction/edit");
		result.addObject("auction", auction);
		result.addObject("message", message);

		return result;
	}
	
	@RequestMapping("/chooseWinningBid")
	public ModelAndView addToGallery(@RequestParam int bidId, @RequestParam int auctionId) {
		ModelAndView result;
		
		try{
			Bid winningBid = bidService.findOne(bidId);
			Auction wonAuction = auctionService.findOne(auctionId);
			if(wonAuction.getWinningBid()!= null){
				result = new ModelAndView("redirect:/auction/customer/listPastByPrincipal.do");
				result.addObject("message", "winningBid.choosing.error");
				return result;
			}else{
				wonAuction.setWinningBid(winningBid);
				auctionService.modify(wonAuction);
				
				winningBid.setWonAuction(wonAuction);
				bidService.modify(winningBid);
				
				result = new ModelAndView("redirect:/auction/customer/listPastByPrincipal.do");
				result.addObject("message", "winningBid.choosing.success");
				return result;
			}
		}catch(Exception e){
			
			result = new ModelAndView("redirect:/auction/customer/listPastByPrincipal.do");
			result.addObject("message", "winningBid.choosing.error");
			return result;
			
		}
	}

}
